package stream;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

public class MapList extends JInternalFrame {
	int row=0;
	HashMap<String, MapData> lhm;
	
	
	Vector<String> userColumn = new Vector<String>();
	
    DefaultTableModel model = new DefaultTableModel(0,0);
    Vector<String> userRow;
	
	private JTextField inSearch;
	private JButton btnSearch;
	private JButton btnAllKey;
	private JButton btnAllData;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private JScrollPane scrollPane_1;
	private JTable userTble;
	
	
	
	public MapList(HashMap<String, MapData> a) {
		super("List", true, true, true, true);
		
		this.lhm=a;
		
		getContentPane().setBackground(Color.GRAY);
		setTitle("List");
		setBounds(100, 100, 720, 442);
		getContentPane().setLayout(null);
		getContentPane().add(getInSearch());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getBtnAllKey());
		getContentPane().add(getBtnAllData());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane_1());

	}

	public void search(){
		if(inSearch.getText().equals("")){
			JOptionPane.showMessageDialog(null, "ID를 입력하세요.");
			return;
		}
		try{
			String ki = inSearch.getText();
			result.append("ID"+"     "+"NAME"+"     "+"PHONE"+"     "+"가입일\n");
			result.append(lhm.get(ki).toString());						
		}catch(Exception ex){
			
		}
	}
	public void search_tabv(){
		if(inSearch.getText().equals("")){
			JOptionPane.showMessageDialog(null, "ID를 입력하세요.");
			return;
		}
		
		//String[] colNames = {"아이디","이름","전화번호","날짜"};
        
		/*userColumn.addElement("ID");
		userColumn.addElement("NAME");
		userColumn.addElement("PHONE");
		userColumn.addElement("DATE");
		
        model = new DefaultTableModel(userColumn,0); */   //테이블 모델 생성
             //테이블 생성   
        
        userRow= new Vector<String>();
        
        userRow.add(lhm.get(inSearch.getText()).userID);
		/*userRow.addElement((String)lhm.get(inSearch.getText()).userID);
		userRow.addElement((String)lhm.get(inSearch.getText()).irum);
		userRow.addElement((String)lhm.get(inSearch.getText()).phone);
		userRow.addElement(String.format("%10s", lhm.get(inSearch.getText()).nal));*/
		
        model = new DefaultTableModel(userColumn,0);    //테이블 모델 생성
		model.addRow(userRow);
		
		
		/*Set <String> sk = lhm.keySet();
		Iterator<String> it = sk.iterator();
		
		if(inSearch.getText().equals(it.hasNext())){
			while(it.hasNext()){			
				String temp = it.next();			
			}
		}*/
		
		
		
		
	}
	
	public void search_tab(){ // Jtable 을 이용한 데이터 출력.
		scrollPane_1.removeAll();

        /*userColumn.addElement("Name");          //열 추가
        userColumn.addElement("Session");*/
        String[] colNames = {"아이디","이름","전화번호","날짜"};
        
        model = new DefaultTableModel(colNames,0);    //테이블 모델 생성
        userTble = new JTable(model);     //테이블 생성        
        
       /* userRow= new Vector<String>();       //내용추가 (행추가)      
        userRow.addElement("홍길동");          
        userRow.addElement("의적");*/
        
        String[][] rows = {{"123","Jint","1234","33-33-33"},{"123","Jint","1234","33-33-33"},{"123","Jint","1234","33-33-33"}};
        
        
        for(int i=0; i<rows.length;i++){
        	model.addRow(rows[i]);
        }
        scrollPane_1.updateUI();
        
                 
	}
	private JTextField getInSearch() {
		if (inSearch == null) {
			inSearch = new JTextField();
			inSearch.setBounds(12, 10, 116, 21);
			inSearch.setColumns(10);
		}
		return inSearch;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//search(); // 기존 Search, textArea 에 결과 출력
					search_tabv();
					
					
				}
			});
			btnSearch.setBounds(140, 9, 116, 23);
		}
		return btnSearch;
	}
	private JButton getBtnAllKey() {
		if (btnAllKey == null) {
			btnAllKey = new JButton("All Key");
			btnAllKey.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					result.setText("");
					int cnt=0;
					Set <String> sk = lhm.keySet();
					Iterator<String> it = sk.iterator();
					
					result.append("=====All Key List=====\n");
					while(it.hasNext()){
						cnt++;
						String temp = it.next();						
						result.append("No."+cnt+"'s Key : "+temp+"\n");
					}
					
				}
			});
			btnAllKey.setBounds(12, 41, 116, 23);
		}
		return btnAllKey;
	}
	private JButton getBtnAllData() {
		if (btnAllData == null) {
			btnAllData = new JButton("All Data");
			btnAllData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					result.setText("");
					result.append("=====All Data List=====\n");
					result.append("ID"+"     "+"NAME"+"     "+"PHONE"+"     "+"가입일\n");
					
					int cnt=0;
					Set <String> sk = lhm.keySet(); // 모든 키 값을 Set에 넘긴다. 순서는 상관 X
					Iterator<String> it = sk.iterator(); // 접근자로 iterator 사용
										
					while(it.hasNext()){
						cnt++;
						String temp = it.next();
						result.append(lhm.get(temp).toString());						
					}					
				}
			});
			btnAllData.setBounds(140, 41, 116, 23);
		}
		return btnAllData;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 74, 242, 299);
			scrollPane.setViewportView(getResult());
			scrollPane.setColumnHeaderView(getLblNewLabel());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel();
		}
		return lblNewLabel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Clear");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText("");
				}
			});
			btnNewButton.setBounds(12, 379, 242, 23);
		}
		return btnNewButton;
	}
	public void setMap(HashMap<String,MapData> m){
		this.lhm=m;
		
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(268, 10, 427, 392);
			scrollPane_1.setViewportView(getTable());
		}
		return scrollPane_1;
	}
	private JTable getTable() {
		if (userTble == null) {
			userTble = new JTable(model);			
		}
		return userTble;
	}
}
